//
//  JiFangCheckViewController.m
//  Qihuo
//
//  Created by Vencoo on 14-1-25.
//  Copyright (c) 2014年 Vencoo. All rights reserved.
//

#import "JiFangCheckViewController.h"
#import "JFCell.h"
@interface JiFangCheckViewController ()
{
    NSMutableArray *receiveArr;
}
@end

@implementation JiFangCheckViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.checkURL = resultOfJFDG;
    self.checkNames = @[@"AB相   电压",@"BC相   电压",@"CA相   电压",@"A相电流",@"B相电流",@"C相电流",@"柜内     温度"];
    [super viewDidLoad];
    self.floorLb.text = @"设备";
    self.floorLb.frame = CGRectMake(30, self.floorLb.frame.origin.y, self.floorLb.frame.size.height*2, self.floorLb.frame.size.height);
    self.title = @"机房电柜巡检记录";
    receiveArr = [NSMutableArray array];
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = [NSString stringWithFormat:@"cell%d%d",indexPath.section,indexPath.row];
    JFCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell)
    {
        cell = [[JFCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        for (int i = 0; i < [[[receiveArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] count]; i ++)
        {
            UILabel *label = [UILabel new];
            label.text = [[[receiveArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row]objectAtIndex:i];
            label.font = [UIFont systemFontOfSize:20];
            CGSize size = [label.text sizeWithFont:label.font constrainedToSize:CGSizeMake(MAXFLOAT, 40)];
            label.frame = CGRectMake((cell.frame.size.width -150)/[[[receiveArr objectAtIndex:indexPath.section]objectAtIndex:indexPath.row] count]*i + 230, (cell.frame.size.height - size.height)/2, size.width, size.height);
            [cell addSubview:label];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.longFloorNameLb.text = [[self.checkMyNames objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
    return cell;
}
#pragma mark - request
- (void)requestDone:(ASIFormDataRequest *)request
{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    NSString *response = [request responseString];
    NSDictionary *queryDic = [response JSONValue];
    NSDictionary *queryDataArr = [queryDic objectForKey:@"data"];
    NSLog( @"%@",queryDataArr);
    NSArray *floorData = [queryDataArr objectForKey:@"floorData"];
    
    if ([[floorData objectAtIndex:0] valueForKey:@"floorName"])
    {
        self.floorNames = [NSMutableArray array];
    }
    for (int i = 0; i < floorData.count; i ++)
    {
        NSDictionary *queryDic = [floorData objectAtIndex:i];
        if ([queryDic objectForKey:@"floorName"]) {
            [self.floorNames addObject:[queryDic valueForKey:@"floorName"]];
        }
        if ([queryDic objectForKey:@"checkData"]) {
            [self.floorCheckDatas addObject:[queryDic valueForKey:@"checkData"]];
        }
    }
    if ([[self.floorCheckDatas firstObject]valueForKey:@"checkName"])
    {
        self.checkMyNames = [NSMutableArray array];
    }
    if ([[self.floorCheckDatas firstObject]valueForKey:@"childData"])
    {
        self.childDatas = [NSMutableArray array];
    }
    for (int i = 0; i < self.floorCheckDatas.count; i ++)
    {
        NSArray *floorDatas = [self.floorCheckDatas objectAtIndex:i];
        NSMutableArray *checkName = [NSMutableArray array];
        NSMutableArray *chidData = [NSMutableArray array];
        NSMutableArray *childResultss = [NSMutableArray array];
        for (int j = 0; j < floorDatas.count; j ++)
        {
            NSDictionary *floorDic = [floorDatas objectAtIndex:j];
            if ([floorDic objectForKey:@"checkName"]) {
                [checkName addObject:[floorDic objectForKey:@"checkName"]];
            }
            if ([floorDic objectForKey:@"childData"]) {
                [chidData addObject:[floorDic objectForKey:@"childData"]];
            }
            NSMutableArray *childResult = [NSMutableArray array];
            for (int k = 0;k<[[floorDic objectForKey:@"childData"] count] ; k ++)
            {
                if ([[[[floorDic objectForKey:@"childData"] objectAtIndex:k]objectForKey:@"result"]rangeOfString:@","].location)
                {
                    NSArray *stringArr = [[[[floorDic objectForKey:@"childData"] objectAtIndex:k]objectForKey:@"result"]componentsSeparatedByString:@","];
                    [childResult addObjectsFromArray:stringArr];
                }
                else
                {
                    [childResult addObject:[[[floorDic objectForKey:@"childData"] objectAtIndex:k]objectForKey:@"result"]];
                }
            }
            [childResultss addObject:childResult];
         }
        [receiveArr addObject:childResultss];
        [self.checkMyNames addObject:checkName];
        [self.childDatas addObject:chidData];
    }
    [self.mainTable reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
